package com.kexinde.wjifen.action;

import java.io.File;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.util.ServletContextAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.kexinde.wjifen.model.Shop;
import com.kexinde.wjifen.model.Shopkeeper;
import com.kexinde.wjifen.service.IShopManager;
import com.kexinde.wjifen.util.CreateUploadPath;
import com.opensymphony.xwork2.ActionSupport;

@Component("Shop")
@Scope("prototype")
public class ShopAction extends ActionSupport implements RequestAware,
		SessionAware, ServletRequestAware, ServletContextAware {
	/**
	 * 修改店铺资料
	 */
	private Shop shop;
	private IShopManager shopManager;
	private Map<String, Object> request;
	private Map<String, Object> session;

	private File shopUpload;// 实际上传文件
	private String shopUploadContentType; // 文件的内容类型
	private ServletContext context;
	private ServletRequest req;

	public String showAllShops() { // 分页显示所有店铺
		List<Shop> shops = this.shopManager.showAllShops();
		request.put("shops", shops);
		return SUCCESS;
	}

	public String showIndexShops() { // 显示首页店铺
		List<Shop> indexShops = this.shopManager.showIndexShops();
		request.put("indexShops", indexShops);
		return SUCCESS;
	}

	public String showShop() { // 显示店铺资料
		Shopkeeper shopkeeper = (Shopkeeper) session.get("shopkeeper");
		shop = this.shopManager.showShop(shopkeeper.getShop().getId());
		request.put("shop", shop);
		return "showshop";
	}

	public String add() { // 添加店铺
		CreateUploadPath cup = new CreateUploadPath(shopUpload,
				shopUploadContentType, context, req, 1);
		try {
			shop.setLogoPath(cup.execute());
		} catch (Exception e) {
			e.printStackTrace();
		}
		// System.out.println(shop.getShopName());
		// System.out.println(shop.getLogoPath());
		// System.out.println(shop.toString());
		// return SUCCESS;
		if (this.shopManager.add(shop))
			return SUCCESS;
		else
			return ERROR;
	}

	public String modify() { // 修改店铺资料
		CreateUploadPath cup = new CreateUploadPath(shopUpload,
				shopUploadContentType, context, req, 1);
		try {
			shop.setLogoPath(cup.execute());
		} catch (Exception e) {
			e.printStackTrace();
		}
		Shopkeeper shopkeeper = (Shopkeeper) session.get("shopkeeper");
		shop.setId(shopkeeper.getShop().getId());
		shop.setRegisterMoney(shopkeeper.getShop().getRegisterMoney());
		if (this.shopManager.modify(shop)) {
			request.put("shopMsg", "修改信息成功");
			return "modifysuccess";
		} else {
			request.put("shopMsg", "修改信息失败");
			return "modifyfail";
		}
	}

	public String invalidateShop() { // 失效店铺
		if (this.shopManager.invalidateShop(shop))
			return SUCCESS;
		else
			return ERROR;
	}

	public String validateShop() { // 生效店铺
		if (this.shopManager.validateShop(shop))
			return SUCCESS;
		else
			return ERROR;
	}

	@Override
	public void setRequest(Map<String, Object> request) {
		// TODO Auto-generated method stub
		this.request = request;
	}

	public Shop getShop() {
		return shop;
	}

	public void setShop(Shop shop) {
		this.shop = shop;
	}

	public IShopManager getShopManager() {
		return shopManager;
	}

	@Resource
	public void setShopManager(IShopManager shopManager) {
		this.shopManager = shopManager;
	}

	@Override
	public void setServletContext(ServletContext context) {
		// TODO Auto-generated method stub
		this.context = context;
	}

	@Override
	public void setServletRequest(HttpServletRequest req) {
		// TODO Auto-generated method stub
		this.req = req;
	}

	public File getShopUpload() {
		return shopUpload;
	}

	public void setShopUpload(File shopUpload) {
		this.shopUpload = shopUpload;
	}

	public String getShopUploadContentType() {
		return shopUploadContentType;
	}

	public void setShopUploadContentType(String shopUploadContentType) {
		this.shopUploadContentType = shopUploadContentType;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

}
